package com.cndw.kungfu.domain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import com.cndw.commons.util.BlankUtils;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.vo.UnionVO;

/**
 * 帮派实体类
 * 
 * @author <a href="mailto:whz-work@163.com">汪洪州</a>
 * 
 * @since 1.0 Create on 2011-5-18
 */
public class Union {

	/**
	 * 帮派Id
	 */
	private int id;

	/**
	 * 帮主Id
	 */
	private long playerId;

	/**
	 * 帮主名字
	 */
	private String playerName;

	/**
	 * 帮派名字
	 */
	private String name;

	/**
	 * 帮派等级
	 */
	private int level;

	/**
	 * 帮派描述信息
	 */
	private String description;

	/**
	 * 帮派公告
	 */
	private String notice;

	/**
	 * 帮派验证方式
	 */
	private short validMode;

	/**
	 * 资源
	 */
	private int resource;

	/**
	 * 帮派资金
	 */
	private int asset;

	/**
	 * 帮派成员数
	 */
	private int memberCount;

	/**
	 * 帮派创建时间
	 */
	private int addTime;
	/**
	 * 帮派神兽等级
	 */
	private int petLv;

	private String skill;

	private Map<Integer, Integer> skillMap;

	/**
	 * 职位权限
	 */
	public static final int GROUPSEND = 1 << 0; // 群发
	public static final int AUDIT = 1 << 1; // 审批
	public static final int INVITE = 1 << 2; // 邀请
	public static final int ASSIGN = 1 << 3; // 任免
	public static final int PULISH = 1 << 4; // 发布公告
	public static final int UPGRADE = 1 << 5; // 升级
	public static final int TECHSKILL = 1 << 7;// 技能升级
	public static final int SETTING = 1 << 8;// 设置帮派
	public static final int EXPEL = 1 << 9; // 开除
	public static final int DISMISS = 1 << 10; // 解散
	public static final int ABDICATE = 1 << 11; // 禅让
	public static final int UNIONPET = 1 << 12; // 帮派神兽

	/**
	 * 计算帮派各角色的权限
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(0); // 成员
		System.out.println(AUDIT | INVITE); // 香主
		System.out.println(AUDIT | INVITE);// 坛主
		System.out.println(AUDIT | INVITE);// 护法
		System.out.println(GROUPSEND | AUDIT | INVITE | ASSIGN | PULISH | UPGRADE);// 长老
		System.out.println(GROUPSEND | AUDIT | INVITE | ASSIGN | PULISH | UPGRADE | TECHSKILL | SETTING | UNIONPET);// 副帮主
		System.out.println(GROUPSEND | AUDIT | INVITE | ASSIGN | PULISH | UPGRADE | TECHSKILL | SETTING | EXPEL | DISMISS | ABDICATE | UNIONPET);// 帮主
	}

	/**
	 * 帮派成员
	 */
	private Map<Long, UnionMember> members = new ConcurrentHashMap<Long, UnionMember>();

	/**
	 * 邀请列表
	 */
	private Map<Long, Integer> inviteMember;

	public Union() {

	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public long getPlayerId() {
		return playerId;
	}

	public void setPlayerId(long playerId) {
		this.playerId = playerId;
	}

	public String getPlayerName() {
		return playerName;
	}

	public void setPlayerName(String playerName) {
		this.playerName = playerName;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getNotice() {
		return notice;
	}

	public void setNotice(String notice) {
		this.notice = notice;
	}

	public short getValidMode() {
		return validMode;
	}

	public void setValidMode(short validMode) {
		this.validMode = validMode;
	}

	public UnionMember removeMember(Long playerId) {
		return members.remove(playerId);
	}

	public void addMember(UnionMember member) {
		getMemberMap().put(member.getPlayerId(), member);
	}

	public void addMembers(List<UnionMember> members) {
		for (UnionMember member : members) {
			getMemberMap().put(member.getPlayerId(), member);
		}
	}

	public List<UnionMember> getMembers() {
		return new ArrayList<UnionMember>(getMemberMap().values());
	}

	public Set<Long> getMemberIds() {
		return getMemberMap().keySet();
	}

	public UnionMember getMember(Long memberId) {
		return getMemberMap().get(memberId);
	}

	private Map<Long, UnionMember> getMemberMap() {
		checkMembers();
		return members;
	}

	private void checkMembers() {
		if (members.isEmpty()) {
			List<UnionMember> memberList = Context.getUnionServer().getUnionMemberByUnionId(getId());
			for (UnionMember member : memberList) {
				members.put(member.getPlayerId(), member);
			}
		}
	}

	public void removeInviteMember(long playerId) {
		if (inviteMember == null) {
			return;
		}

		inviteMember.remove(playerId);
	}

	public boolean containsInviteMember(long playerId) {
		if (inviteMember == null) {
			return false;
		}

		return inviteMember.containsKey(playerId);
	}

	public synchronized void putInviteMember(long playerId, int timestamp) {
		if (inviteMember == null) {
			inviteMember = new HashMap<Long, Integer>();
		}
		inviteMember.put(playerId, timestamp);
	}

	public int getAsset() {
		return asset;
	}

	public void alertAsset(int asset) {
		synchronized (this) {
			if (asset > 0) {
				UnionConfig unionConfig = Context.getUnionServer().getUnionConfigByLevel(getLevel());
				if ((getAsset() + asset) > unionConfig.getAssetLimit()) {
					setAsset(unionConfig.getAssetLimit());
				} else {
					setAsset(getAsset() + asset);
				}
			} else {
				setAsset(getAsset() + asset);
			}
		}
	}

	public void setAsset(int asset) {
		this.asset = asset;
	}

	public int getMemberCount() {
		return memberCount;
	}

	public void setMemberCount(int memberCount) {
		this.memberCount = memberCount;
	}

	public int getAddTime() {
		return addTime;
	}

	public void setAddTime(int addTime) {
		this.addTime = addTime;
	}

	public int getResource() {
		return resource;
	}

	public void setResource(int resource) {
		this.resource = resource;
	}

	public String getSkill() {

		if (!BlankUtils.isBlank(skillMap)) {
			StringBuilder buffer = new StringBuilder();
			Iterator<Integer> iterator = skillMap.keySet().iterator();
			while (iterator.hasNext()) {
				buffer.append(skillMap.get(iterator.next()));
				if (iterator.hasNext()) {
					buffer.append(",");
				}
			}
			skill = buffer.toString();
		}

		return skill;
	}

	public void setSkill(String skill) {
		this.skill = skill;

		if (!BlankUtils.isBlank(skill)) {

			if (skillMap == null) {
				skillMap = new HashMap<Integer, Integer>();
			} else {
				skillMap.clear();
			}

			String[] skills = skill.split(",");
			for (int i = 0, j = skills.length; i < j; i++) {
				if (i == 7) {
					this.skillMap.put(i + 2, Integer.valueOf(skills[i]));
				} else {
					this.skillMap.put(i + 1, Integer.valueOf(skills[i]));
				}
			}
		}
	}

	public void setSkillLevel(int type, int level) {
		if (skillMap == null) {
			skillMap = new HashMap<Integer, Integer>();
			for (int i = 1; i <= 7; i++) {
				skillMap.put(i, 0);
			}
			skillMap.put(9, 0);
		}
		skillMap.put(type, level);
	}

	public Map<Integer, Integer> getSkillMap() {
		return skillMap;
	}

	public void setSkillMap(Map<Integer, Integer> skillMap) {
		this.skillMap = skillMap;
	}

	public void broadcast(Object obj, String actionName) {
		// 底层发送时候做了不在线检测
		Context.getTcpServer().broadcast(obj, actionName, getMemberIds());
	}

	public UnionVO toUnionVO() {
		UnionVO unionVO = new UnionVO();
		unionVO.setId(getId());
		unionVO.setLevel(getLevel());
		unionVO.setName(getName());
		unionVO.setPlayerId(getPlayerId());
		unionVO.setPlayerName(getPlayerName());
		unionVO.setValidMode(getValidMode());
		unionVO.setAsset(getAsset());
		unionVO.setNotice(getNotice());
		unionVO.setMemberCount(getMemberCount());
		unionVO.setResource(getResource());
		unionVO.setAddTime(getAddTime());
		unionVO.setStatus(Context.getPlayerServer().isOnline(getPlayerId()));

		UnionConfig config = Context.getCacheCenter().getUnionConfigByLevel(this.getLevel());
		unionVO.setMemberCountLimit(config.getMemberLimit());

		config = Context.getCacheCenter().getUnionConfigByLevel(this.getLevel());
		unionVO.setAssetLimit(config.getAssetLimit());
		return unionVO;
	}

	public int getPetLv() {
		return petLv;
	}

	public void setPetLv(int petLv) {
		this.petLv = petLv;
	}
}
